package com.xx.xunyou.serviceImpl;

import com.xx.springBootUtil.beanUtils.BeanUtilsExtends;
import com.xx.springBootUtil.excption.BaseException;
import com.xx.springBootUtil.util.MessageUtil;
import com.xx.springBootUtil.util.ObjectHelper;
import com.xx.springBootUtil.util.UUIDHelper;
import com.xx.springBootUtil.zzcache.ZZCacheUtils;
import com.xx.util.result.JsonUtil;
import com.xx.util.util.JsonUtils;
import com.xx.xunyou.dao.user.XunyouUserMapper;
import com.xx.xunyou.entity.user.XunyouUser;
import com.xx.xunyou.entity.user.vo.LoginVo;
import com.xx.xunyou.entity.user.vo.XunyouUserBaseVo;
import com.xx.xunyou.service.XunyouUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @program: xunyou-parent
 * @author: zhl
 * @create: 2019-05-04
 **/
@Service
public class XunyouUserServiceImpl implements XunyouUserService {


    /**
     * 登录前缀
     */
    public static final String TOKEN = "xunyou_";

    /**
     * 登录过期时间
     */
    public static final Long loginExpTime = 7*24*60*60*1000l;

    @Autowired
    private XunyouUserMapper xunyouUserMapper;

    @Override
    @Transactional
    public void insert(XunyouUser xunyouUser) {
        String userName = xunyouUser.getUserName();
        XunyouUser xunyouUse1 = xunyouUserMapper.queryByUserName(userName);
        if (xunyouUse1!=null){
            throw new BaseException("该用户名已被使用！");
        }
        xunyouUserMapper.insertSelective(xunyouUser);
    }

    @Override
    @Transactional
    public LoginVo login(String userName, String password) {
        XunyouUser xunyouUse1 = xunyouUserMapper.queryByUserName(userName);
        if(ObjectHelper.isEmpty(xunyouUse1)){
            throw new BaseException("账号或者密码错误！");
        }else if(xunyouUse1.getIsDelete() == 2) {
            throw new BaseException("账号已被禁用，有问题请联系管理员");
        }else {
            if (xunyouUse1.getUserType()!=2){
                throw new BaseException("账号或者密码错误！");
            }
            if (!MessageUtil.encodeMD5(password).equals(xunyouUse1.getPassWord())){
                throw new BaseException("账号或者密码错误！");
            }
            String uuId = UUIDHelper.createUUId();
            LoginVo loginVo = new LoginVo();
            loginVo.setId(xunyouUse1.getId());
            loginVo.setNickname(xunyouUse1.getNickname());
            loginVo.setUserImage(xunyouUse1.getUserImage());
            loginVo.setUserName(xunyouUse1.getUserName());
            xunyouUse1.setLoginTime(new Date());
            xunyouUserMapper.updateByPrimaryKeySelective(xunyouUse1);
            String token = TOKEN+xunyouUse1.getUserId()+"_"+uuId;
            loginVo.setToken(token);
            //删除缓存
            ZZCacheUtils.containDelete(TOKEN+xunyouUse1.getUserId()+"_");
            //存入缓存
            ZZCacheUtils.set(token,JsonUtils.objectToJson(loginVo),loginExpTime);
            return loginVo;
        }
    }



    @Override
    public XunyouUserBaseVo queryAuthor(Integer userId) {
        XunyouUser xunyouUser = xunyouUserMapper.selectByPrimaryKey(userId);
        if(xunyouUser!=null){
            XunyouUserBaseVo author = new XunyouUserBaseVo();
            BeanUtilsExtends.copyProperties(author, xunyouUser);
            return author;
        }
        return null;
    }
}
